package com.dj.mingkong.common.util;

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class HashUtils {
    private HashUtils() {
    }

    public static long getHash(String key) {
        HashFunction hashFunction = Hashing.murmur3_32();
        HashCode hashCode = hashFunction.hashString(key, StandardCharsets.UTF_8);
        int hash = hashCode.asInt();
        if (hash < 0) {
            hash = Math.abs(hash);
        }

        return (long) hash;
    }

    public static String getStreamHash(InputStream fis) throws IOException {
        return DigestUtils.md5Hex(fis);
    }
}
